use glutin::event_loop::{EventLoop, ControlFlow};
use glutin::event::WindowEvent;
use glutin::window::WindowBuilder;
use glutin::ContextBuilder;
use std::mem::zeroed;
use zert::graphics::RectBatcher;
use zmath::Vec4;

fn main() {
    let el = EventLoop::new();

    let context = {
        let wb = WindowBuilder::new()
            .with_title("From glutin");

        ContextBuilder::new()
            .build_windowed(wb, &el)
            .unwrap()
    };

    let context = unsafe { context.make_current().unwrap() };

    zert::load(|addr| context.get_proc_address(addr));

    let size = context.window().inner_size();

    let mut batcher = RectBatcher::new((size.width as f32, size.height as f32));

    el.run(move |e, _, cf| {
        *cf = ControlFlow::Poll;

        match e {
            glutin::event::Event::WindowEvent { event, .. } => {
                if let WindowEvent::CloseRequested = event {
                    *cf = ControlFlow::Exit;
                }
            }
            glutin::event::Event::MainEventsCleared => {
                zert::gl_clear_color(1.0, 1.0, 1.0, 1.0);
                zert::gl_clear(gl::COLOR_BUFFER_BIT);

                batcher.begin();
                batcher.draw_rect(0.0, 0.0, 100.0, 100.0, &[Vec4(1.0, 0.0, 0.0, 1.0)]);
                batcher.present(None);
            }
            glutin::event::Event::RedrawEventsCleared => {
                context.swap_buffers().unwrap();
            }
            _ => ()
        }
    });
}